// 验证模块
const Joi = require('joi');
// 床模块
const {
	Facility,
	badFacility
} = require('../../../model/Facility');

module.exports = async (req, res) => {
	let fcl = await Facility.findOne({
		title: req.fields.title
	});
	if (fcl) return res.sendResult(null, 400, '已拥有的宝可梦！换一只收服吧~');

	req.fields._id = req.params['id'];
	// 数据格式没有通过验证
	if (res.isFakeID(req.fields._id)) return;
	if (badFacility(req.fields, req, res)) return;
	// 通过验证
	// 更新设施信息
	// new: true 返回修改后的文档 默认值为false 返回原始文档
	fcl = await Facility.findByIdAndUpdate(req.fields._id, {
		$set: req.fields
	}, {
		new: true
	});
	// 响应
	return res.sendResult(fcl, 200, '更新设施成功');
};